package com.example.stock.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.stock.eo.StockDetailEo;
import com.example.stock.mapper.StockDetailMapper;
import com.example.stock.vo.DataVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zhuchunfeng
 * @Description
 * @Date: 2021/1/2 14:38
 * Copyright (c) 2019 北京新媒传信科技有限公司
 */

@Service
public class StockDetailService extends ServiceImpl<StockDetailMapper, StockDetailEo> {
    @Resource
    private StockDetailMapper stockDetailMapper;

    int analysisRepeat = 0;

    public String analysisFile() {
        if (analysisRepeat++ > 0) {
            return "repeat!";
        }
        String hs300Files = "C:\\Users\\zhu.chunfeng\\Desktop\\文件夹\\project\\gitee\\demo-parent\\7stock\\src\\main\\resources\\hs300_utf8";
        File file = new File(hs300Files);
        File[] files = file.listFiles();
//        Arrays.stream(files).forEach(f -> test(f));
        return "success";
    }

    public List<DataVo> getRowData() {
        List<StockDetailEo> stockDetailEos = stockDetailMapper.selectList(
                Wrappers.<StockDetailEo>lambdaQuery().orderByAsc(StockDetailEo::getTime));
        List<DataVo> closeData = stockDetailEos.stream().map(t->{
            DataVo dataVo = new DataVo(t.getTime(),t.getClose());
            return dataVo;
        }).collect(Collectors.toList());
        return closeData;
    }
    public List<DataVo> getRowData30(){
        List<DataVo> rowData = getRowData();
        rowData.removeIf(r -> !Arrays.asList(0, 30).contains(r.getTime().getMinute()));
        return rowData;
    }

    public void test(File fileHs300) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileHs300));
            String line;
            while ((line = reader.readLine()) != null) {
                //大富翁数据中心_市场代码,证券代码,日期,开盘价,最高价,最低价,收盘价,成交量,成交金额
                String[] item = line.split(",");//一行数组
                if (item[1].contains("代码")) {
                    continue;
                }
                StockDetailEo eo = new StockDetailEo();
                eo.setCode(item[1]);
                eo.setOpen(new BigDecimal(item[3]));
                eo.setClose(new BigDecimal(item[6]));
                eo.setHigh(new BigDecimal(item[4]));
                eo.setLow(new BigDecimal(item[5]));
                eo.setAmount(new BigDecimal(item[8]));
                eo.setVol(new BigDecimal(item[7]));
                eo.setTime(DateUtil.parseLocalDateTime(item[2]));
                stockDetailMapper.insert(eo);
            }
        } catch (Exception e) {
            //在命令行打印异常信息在程序中出错的位置及原因。
            e.printStackTrace();
        }
    }

}
